/*-----------------------------------------------------------------------------

 Copyright 2017 Hopsan Group

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.


 The full license is available in the file LICENSE.
 For details about the 'Hopsan Group' or information about Authors and
 Contributors see the HOPSANGROUP and AUTHORS files that are located in
 the Hopsan source code root directory.

-----------------------------------------------------------------------------*/

#ifndef AEROATMOSPHERE_HPP_INCLUDED
#define AEROATMOSPHERE_HPP_INCLUDED

#include <iostream>
#include "ComponentEssentials.h"
#include "ComponentUtilities.h"
#include "math.h"

//!
//! @file AeroAtmosphere.hpp
//! @author Petter Krus <petter.krus@liu.se>
//! @date Mon 14 Oct 2013 10:33:48
//! @brief model of standard atmosphere
//! @ingroup AeroComponents
//!
//==This code has been autogenerated using Compgen==
//from 
/*{, C:, HopsanTrunk, HOPSAN++, CompgenModels}/AeroComponents.nb*/

using namespace hopsan;

class AeroAtmosphere : public ComponentSignal
{
private:
     double g0;
     double rhos;
     double a;
     double R;
     double gamma;
     double Ts;
     double p0s;
     double htp;
     double Ttp;
     double ptp;
     double rhotp;
     double e;
     int mNstep;
//==This code has been autogenerated using Compgen==
     //inputVariables
     double ha;
     //outputVariables
     double rhoa;
     double Ta;
     double p0;
     double Vsound;
     //Expressions variables
     //Delay declarations
//==This code has been autogenerated using Compgen==
     //inputVariables pointers
     double *mpha;
     //inputParameters pointers
     double *mpg0;
     double *mprhos;
     double *mpa;
     double *mpR;
     double *mpgamma;
     double *mpTs;
     double *mpp0s;
     double *mphtp;
     double *mpTtp;
     double *mpptp;
     double *mprhotp;
     double *mpe;
     //outputVariables pointers
     double *mprhoa;
     double *mpTa;
     double *mpp0;
     double *mpVsound;
     EquationSystemSolver *mpSolver;

public:
     static Component *Creator()
     {
        return new AeroAtmosphere();
     }

     void configure()
     {
//==This code has been autogenerated using Compgen==

        mNstep=9;

        //Add ports to the component
        //Add inputVariables to the component
            addInputVariable("ha","Altitude","m",0.,&mpha);

        //Add inputParammeters to the component
            addInputVariable("g0", "Gravitation acceleration", "m/s^2", \
9.81,&mpg0);
            addInputVariable("rhos", "Density at sea level", "kg/m3", \
1.225,&mprhos);
            addInputVariable("a", "", "", -0.0065,&mpa);
            addInputVariable("R", "", "", 287,&mpR);
            addInputVariable("gamma", "", "", 1.4,&mpgamma);
            addInputVariable("Ts", "Temperature at sea level", "K", \
288.16,&mpTs);
            addInputVariable("p0s", "", "Pa", 101300.,&mpp0s);
            addInputVariable("htp", "Onset of tropopaus", "m", \
11000.,&mphtp);
            addInputVariable("Ttp", "", "K", 216.66,&mpTtp);
            addInputVariable("ptp", "", "Pa", 22610.,&mpptp);
            addInputVariable("rhotp", "", "kg/m3", 0.363649,&mprhotp);
            addInputVariable("e", "e", "", 2.71828,&mpe);
        //Add outputVariables to the component
            addOutputVariable("rhoa","The average density at altitude \
h","kg/m3",1.25,&mprhoa);
            addOutputVariable("Ta","Average temperature at altitude \
h","K",273.,&mpTa);
            addOutputVariable("p0","The average ressure at altitude \
h","Pa",100000.,&mpp0);
            addOutputVariable("Vsound","Speed of sound at altitude \
h","m/s",340.,&mpVsound);

//==This code has been autogenerated using Compgen==
        //Add constantParameters
     }

    void initialize()
     {
        //Read port variable pointers from nodes

        //Read variables from nodes

        //Read inputVariables from nodes
        ha = (*mpha);

        //Read inputParameters from nodes
        g0 = (*mpg0);
        rhos = (*mprhos);
        a = (*mpa);
        R = (*mpR);
        gamma = (*mpgamma);
        Ts = (*mpTs);
        p0s = (*mpp0s);
        htp = (*mphtp);
        Ttp = (*mpTtp);
        ptp = (*mpptp);
        rhotp = (*mprhotp);
        e = (*mpe);

        //Read outputVariables from nodes
        rhoa = (*mprhoa);
        Ta = (*mpTa);
        p0 = (*mpp0);
        Vsound = (*mpVsound);

//==This code has been autogenerated using Compgen==


        //Initialize delays

     }
    void simulateOneTimestep()
     {
        //Read variables from nodes

        //Read inputVariables from nodes
        ha = (*mpha);

        //LocalExpressions

          //Expressions
          Ta = (a*ha + Ts)*onNegative(ha - htp) + (a*htp + Ts)*onPositive(ha \
- htp);
          rhoa = rhos*Power(Ta/Ts,-1 - g0/(a*R))*onNegative(ha - htp) + \
(rhotp*onPositive(ha - htp))/Power(e,(g0*(ha - htp))/(R*Ttp));
          p0 = (p0s*onNegative(ha - htp))/Power(Ta/Ts,g0/(a*R)) + \
(ptp*onPositive(ha - htp))/Power(e,(g0*(ha - htp))/(R*Ttp));
          Vsound = Sqrt(gamma*R*Ta);

        //Calculate the delayed parts


        //Write new values to nodes
        //outputVariables
        (*mprhoa)=rhoa;
        (*mpTa)=Ta;
        (*mpp0)=p0;
        (*mpVsound)=Vsound;

        //Update the delayed variabels

     }
    void deconfigure()
    {
        delete mpSolver;
    }
};
#endif // AEROATMOSPHERE_HPP_INCLUDED
